#include<iostream>
#include<algorithm>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int main()
{
    // zdl:: 现在这道题目就是需要的就是对数学取模运算的了解
    int n; cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    n = unique(a + 1, a + 1 + n) - (a + 1);
    if (n == 1) cout << -1 << endl;
    else if (n == 2) cout  << a[2] % a[1] << endl;
    else cout << max(a[n - 2], a[n] % a[n - 1]) << endl;
    return 0;
}